<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/8/28
 * Time: 11:20
 */

namespace app\www\controller;

use think\Session;
use think\Db;

class Index extends Common
{
    //wap Index/index 首页
    public function index(){
        $access_token=Session::get("access_token");
        $data=array(
            "module"=>'Member',
            "interface"=>'1003',
            "version"=>'1.0.0',
            "data"=>array(
                "access_token"=>$access_token
            )
        );
        $res=curl_post(config('self_url'),json_encode($data));
//var_dump(json_decode($res)->result_data);die;
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            $this->error($result_info);
        }

        $result_data=json_decode($res,true)['result_data'];
        foreach ($result_data['date_arr'] as $key=>$value)
        {
            $result_data['date_arr'][$key]['value']=floatval($value['value']);
        }
        $this->assign("swiper",$result_data['banner_list']);
        $this->assign("news_list",$result_data['news_list']);
        $this->assign("notice",$result_data['notice_list']);
        $this->assign("active_list",$result_data['active_list']);
        $this->assign("bussiness_list",$result_data['bussiness_list']);
        $this->assign("date_arr",json_encode($result_data['date_arr']));
        $this->assign("consortium_list",$result_data['consortium_list']);
//var_dump($result_data['notice_list']);die;

        return $this->fetch();
    }

    /*公告列表*/
    public function noticeList()
    {
        $data=input();
        $type=empty($data['type'])?1:input('type');
        $this->assign('type',$type);
        $page=empty($data['page'])?1:input('page');
        $page_size=empty($data['page_size'])?10:input('page_size');

        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Active',
            "interface"=>'1006',
            "version"=>'1.0.0',
            "data"=> [
                "page_size"=> $page_size,
                "type"=>$type,
                "page"=> $page,
                "access_token"=> $access_token
            ]
        );
        if(request()->isPost())
        {
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                return 0;
            }else{
                $result_data=json_decode($res,true)['result_data']['list'];
                return json_encode($result_data);
            }

        }else{
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }
            $result_data=json_decode($res,true)['result_data'];
            $this->assign("list",$result_data['list']);
            return $this->fetch();
        }
    }
    /*公告详情*/
    public function noticeInfo(){
        $id=input('id');
        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Active',
            "interface"=>'1007',
            "version"=>'1.0.0',
            "data"=> [
                "notice_id"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }else{
            $result_info=json_decode($res,true)['result_data'];
            $this->assign("info",$result_info);
            return $this->fetch();
        }

    }
    /*新闻列表*/
    public function newsList()
    {
        $data=input();
        $page=empty($data['page'])?1:input('page');
        $page_size=empty($data['page_size'])?5:input('page_size');
        $category_id=empty($data['category_id'])?1:$data['category_id'];
        $access_token=Session::get("access_token");
        /*获取分类*/
        $category=Db::name('content_category')
            ->where('is_visible',1)
            ->order('display_order')
            ->select();
        $this->assign("category",$category);

        $data1=array(
            "module"=>'Member',
            "interface"=>'1010',
            "version"=>'1.0.0',
            "data"=> [
                "page_size"=> $page_size,
                "page"=> $page,
                "bussiness_id"=> 0,
                "category_id"=> $category_id,
                "access_token"=> $access_token
            ]
        );

        if(request()->isPost())
        {
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                return 0;
            }else{
                $result_data=json_decode($res,true)['result_data']['list'];
                return json_encode($result_data);
            }

        }else{
            $res=curl_post(config('self_url'),json_encode($data1));
            $result_code=json_decode($res,true)['result_code'];
            $result_info=json_decode($res,true)['result_info'];
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }
            $result_data=json_decode($res,true)['result_data'];
            $this->assign("list",$result_data['list']);
            $this->assign("cid",$category_id);
            return $this->fetch();
        }

    }
    /*连新闻详情*/
    public function newsInfo(){
        $id=input('id');
        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Member',
            "interface"=>'1011',
            "version"=>'1.0.0',
            "data"=> [
                "news_id"=> $id,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            return $this->error($result_info);

        }else{
            $result_info=json_decode($res,true)['result_data'];
            $this->assign("info",$result_info);
            return $this->fetch();
        }

    }


}